<template>
  <main class="main-container" v-loading="isLoading">
    <DeviceSearchBar></DeviceSearchBar>
    <div class="reader-container">
      <div>
        <el-text>设备逻辑名：</el-text>
        <el-select
          style="margin-top: 4px"
          placeholder="请选择设备逻辑名"
          v-model="deviceReader.deviceName"
        >
          <el-option
            v-for="typeItem in deviceStore.deviceNames"
            :key="typeItem"
            :value="typeItem"
            :label="typeItem"
          ></el-option>
        </el-select>
      </div>
      <div style="margin-top: 10px">
        <el-text>数据类型：</el-text>
        <el-select
          style="margin-top: 4px"
          placeholder="请选择数据类型"
          v-model="deviceReader.dataType"
        >
          <el-option
            v-for="typeItem in deviceStore.dataTypes"
            :key="typeItem"
            :value="typeItem"
            :label="typeItem"
          ></el-option>
        </el-select>
      </div>
      <div style="margin-top: 10px">
        <el-text>设备点位地址：</el-text>
        <el-input
          style="margin-top: 4px"
          v-model="deviceReader.address"
          maxlength="15"
          placeholder="请输入设备点位地址"
          type="text"
        ></el-input>
      </div>
      <el-button style="width: 100%; margin-top: 10px" type="primary" @click="onRead()">
        读取数据
      </el-button>
      <div style="margin-top: 10px">
        <el-text>读取的数据值：</el-text>
        <el-input
          style="margin-top: 4px"
          v-model="deviceReader.readValue"
          type="text"
          readonly
        ></el-input>
      </div>
    </div>
  </main>
</template>

<script setup lang="ts">
import DeviceSearchBar from '@/components/device/device-search-bar.vue'
import { useDeviceManagerStore } from '@/stores/device/manager'
import { useDeviceReaderStore } from '@/stores/device/reader'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { DeviceServerClient } from '@/api/axios/device-server-client'

const client = new DeviceServerClient(import.meta.env.VITE_DEVICE_SERVER_BASE_URL)
const deviceStore = useDeviceManagerStore()
const deviceReader = useDeviceReaderStore()
const isLoading = ref(false)

async function onRead() {
  if (deviceReader.deviceName == '' || deviceReader.deviceName == null) {
    ElMessage.error('请选择设备逻辑名！')
  } else if (deviceReader.dataType == '' || deviceReader.dataType == null) {
    ElMessage.error('请选择数据类型！')
  } else if (deviceReader.address == '' || deviceReader.address == null) {
    ElMessage.error('请输入有效的地址！')
  } else {
    isLoading.value = true
    let result = await client.readValueAsync({
      deviceId: deviceStore.currentNodeId,
      task: {
        deviceName: deviceReader.deviceName,
        address: deviceReader.address,
        dataType: deviceReader.dataType
      }
    })

    isLoading.value = false

    if (result.isSuccess) {
      deviceReader.readValue = result.result
      ElMessage.success('读取完成！')
    } else {
      ElMessage.error(result.message)
    }
  }
}
</script>

<style scoped>
.main-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

.reader-container {
  height: 100%;
  padding: 20px 30px 20px;
  background-color: #fff;
  border: 1px solid #ddd;
  margin-top: 10px;
  border-radius: 5px;
}
</style>
